package main

import "C"
import (
	"fmt"
	"strings"

	"sort"

	"gitee.com/huanghua_2017/blade/client-cgo/clientcore"
)

//export GetProxyListen
func GetProxyListen(cstr **C.char) {
	*cstr = C.CString(clientcore.DefaultCore.Cfg.ProxyAddress)
}

//export GetPACListen
func GetPACListen(cstr **C.char) {
	*cstr = C.CString(clientcore.DefaultCore.Cfg.PACListen)
}

//export GetServerAddr
func GetServerAddr(cstr **C.char) {
	*cstr = C.CString(clientcore.DefaultCore.Cfg.ServerAddrs[clientcore.DefaultCore.Cfg.Index])
}

//export GetServerIndex
func GetServerIndex(cstr **C.char) {
	*cstr = C.CString(clientcore.DefaultCore.Cfg.Index)
}

//export GetServerIndexList
func GetServerIndexList(cstr **C.char) {
	var arr []string
	for k := range clientcore.DefaultCore.Cfg.ServerAddrs {
		arr = append(arr, k)
	}
	sort.Strings(arr)
	*cstr = C.CString(strings.Join(arr, "\n"))
}

//export StartSlave
func StartSlave(laddr *C.char, raddr *C.char) *C.char {
	err := clientcore.DefaultCore.StartSlave(C.GoString(laddr), C.GoString(raddr))
	if err == nil {
		return nil
	}
	return C.CString(err.Error())
}

//export StopSlave
func StopSlave(laddr *C.char) *C.char {
	err := clientcore.DefaultCore.StopSlave(C.GoString(laddr))
	if err == nil {
		return nil
	}
	return C.CString(err.Error())
}

//export ChangeServerIndex
func ChangeServerIndex(index *C.char) {
	str1 := C.GoString(index)
	clientcore.DefaultCore.SwitchProxy(str1)
}

//export RunCore
func RunCore(logcstr *C.char, cfgcstr *C.char) {
	str1 := C.GoString(logcstr)
	str2 := C.GoString(cfgcstr)
	clientcore.StartClient(str1, str2)
}

func main() {
	fmt.Println("start")
}
